//
//  MoreMovieViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 14-12-12.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "MoreMovieViewController.h"
#import "SingleMovieDetailViewController.h"

static const CGFloat MOREMOVIElISTCELL_HEIGHT = 105;

@interface MoreMovieViewController ()

@property (nonatomic,assign)int currentIndex;

@property (nonatomic,strong)NSMutableArray* moviesList;

@end

@implementation MoreMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.currentIndex = -1;
    self.tableView.backgroundColor = [Common colorWithRGB:0Xffffff];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    [self setEnableRefresh:YES];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.title = @"影片";
    [self getMoreMovieList];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Protocol
- (void)getMoreMovieList
{
    [[ProtocolManager shareInstance]getStoresList:self.type returnCount:STORE_LIST_RETURN_COUNT beginNo:self.currentIndex+1 area:nil star:nil storeType:@"电影院" target:self selector:@selector(getMoreMovieListCallback:)];
}

#pragma mark - Callback
- (void)getMoreMovieListCallback:(NSDictionary*)dic
{
    NSLog(@"getMoreMovieListCallback %@",dic);
    if (self.currentIndex == -1) {
        self.moviesList = [NSMutableArray array];
    }
    NSArray *moviesList = [Common valueForKey:P_MOVIES dic:dic];
    if ([moviesList isKindOfClass:[NSArray class]])
    {
        [self.moviesList addObjectsFromArray:moviesList];
        self.currentIndex += self.moviesList.count;
    }
    [self.tableView reloadData];
}

#pragma mark - Cell
- (void)moreMovieListCell:(UITableViewCell*)cell index:(int)index
{
    if ( index >= self.moviesList.count) {
        return;
    }
    NSDictionary *cellDate = self.moviesList[index];
    NSString *imgUrl = [Common valueForKey:P_COVER dic:cellDate];
    UIImageView *movieImage = (UIImageView*)[cell viewWithTag:1001];
    [movieImage setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    
    NSString *name = [Common valueForKey:P_NAME dic:cellDate];
    UILabel *nameLabel = (UILabel*)[cell viewWithTag:1002];
    nameLabel.text = name;
    
    NSString *rating = [Common valueForKey:P_RATING dic:cellDate];
    UILabel *ratingLabel = (UILabel*)[cell viewWithTag:1006];
    ratingLabel.text = rating;
    
}

#pragma mark - TableView
//返回table中有多少个组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.moviesList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    switch (indexPath.section) {
        case 0:
            cell = [tableView dequeueReusableCellWithIdentifier:@"MoreMovieListCell"];
            if (cell == nil) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"MoreMovieListCell"  owner:self options:nil][0];
            }
            [self moreMovieListCell:cell index:indexPath.row];
            break;
            
        default:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            }
            break;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return MOREMOVIElISTCELL_HEIGHT;
    }
    return 0;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _moviesList[indexPath.row];
    SingleMovieDetailViewController *SingleMovieDetailViewCtrl = [[SingleMovieDetailViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    SingleMovieDetailViewCtrl.goodId = [[Common valueForKey:P_ID dic:dic] integerValue];
    [self.navigationController pushViewController:SingleMovieDetailViewCtrl animated:YES];
}


#pragma mark - PullTableViewDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    NSLog(@"refresh...");
    self.currentIndex = -1;
    //[self getMovieDetail];
}

-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    NSLog(@"loadMore...");
    //[self getMovieDetail];
}

- (void)resetPullTableViewState
{
    [self setPullTableIsRefreshing:NO];
    [self setPullTableIsLoadMore:NO];
}


@end
